package cn.fql.user.service.impl;

import cn.fql.basic.exception.BusinessException;
import cn.fql.basic.util.Md5Utils;
import cn.fql.basic.util.StrUtils;
import cn.fql.org.domain.Employee;
import cn.fql.user.domain.Logininfo;
import cn.fql.user.domain.User;
import cn.fql.user.mapper.LogininfoMapper;
import cn.fql.user.mapper.UserMapper;
import cn.fql.user.service.IUserService;
import cn.fql.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void save(User user) {

        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        //创建logininfo对象
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//1是用户 0是管理员
        logininfo.setDisable(true);
        //将logininfo对象添加到数据库并生成自增长id
        logininfoMapper.save(logininfo);//自增长id
        //将logininfo对象设置到user对象里面去
        user.setLogininfoId(logininfo.getId());
        //添加到数据库
        super.save(user);
        //userMapper.save(user); 
    }

    @Override
    public void del(Long id) {
        //根据id 查询user对象
        User user = userMapper.findById(id);
        //根据user对象里面的logininfoid 删除logininfo信息
        logininfoMapper.del(user.getLogininfoId());
        //删除user信息
        super.del(id);
    }

    @Override
    public void update(User user) {
        /**
         *修改的时候页面中不会传递 Logininfo_id() 和 shop_id
         * 这两个参数 如果不处理点击修改会变成null
         */
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);

        //查询原来的employee对象
        User oldUser = userMapper.findById(user.getId());
        user.setLogininfoId(oldUser.getLogininfoId());

        //更新数据t_logininfo
        super.update(user);
        //从原来的对象里面拿到loginifo 的id
        //更新t_logininfo
        Logininfo oldLogininfo = logininfoMapper.findById(oldUser.getLogininfoId());
        BeanUtils.copyProperties(user,oldLogininfo);
        //属性同名原则会把员工的id拷贝给登录信息对象的id
        oldLogininfo.setId(oldUser.getLogininfoId());
        logininfoMapper.update(oldLogininfo);
    }

    @Override
    public void phoneRegister(Map<String, String> map) {
        String phone = map.get("phone");
        String phoneCode = map.get("phoneCode");
        String password = map.get("password");
        String passwordRepeat = map.get("passwordRepeat");

        //一、首先进行空值校验
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneCode) ||
           StringUtils.isEmpty(password) ||StringUtils.isEmpty(passwordRepeat)){
            throw new BusinessException("信息不能为空");
        }
        //二、再进行 密码验证两次输入是否一致
        if(!password.equals(passwordRepeat)){
            throw new BusinessException("两次输入的密码不一致");
        }
        //三、在进行对手机验证码是否过期 和是否正确进行验证
        Object obj = redisTemplate.opsForValue().get("register:" + phone);
        if(obj == null){
            throw new BusinessException("手机验证码过期");
        }
        if(!obj.toString().split(":")[0].equals(phoneCode)){
            throw new BusinessException("手机验证码错误");
        }
        //四：添加到数据库，密码加密加盐
        User user = map2User(phone,password);
        Logininfo logininfo = user2Logininfo(user);
        //1.先对t_logininfo进行添加
        logininfoMapper.save(logininfo);
        //2.再添加t_user
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //底层会按照属性同名原则进行属性的拷贝：id,username,phone,email,salt,password
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);
        return logininfo;
    }
    //将map中的数据转成一个User对象：当一个对象转成另外一个对象的时候，并且代码量比较多
    //可以通过obj2Obj的方式单独把这个业务提取出来。提升代码的可读性。代码不臃肿
    private User map2User(String phone, String password) {
        User user = new User();
        user.setUsername(phone);
        user.setPhone(phone);
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(password + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        user.setState(1);
        return user;
    }
}
